﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.Domain.Uow;
using Mt.Site.Core.Storage;
using Mt.Site.Core.Synchronization.Data;
using System;

namespace Mt.Site.Core.Synchronization.Storage
{
    public class FileObjectDataStoreManager:DomainService,IFileObjectDataStoreManager
    {

        private readonly IRepository<FileObject, Guid> _fileObjectRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public FileObjectDataStoreManager(
            IRepository<FileObject, Guid> fileObjectRepository,
            IUnitOfWorkManager unitOfWorkManager)
        {
            _fileObjectRepository = fileObjectRepository;
            _unitOfWorkManager = unitOfWorkManager;
        }

        public Guid CreateFileObjectSync(FileObject fromEntity, DataProcessingStore input)
        {
            var newEntity = new FileObject();
            var id = Guid.NewGuid();
            newEntity.Id = id;
            newEntity.AutoSynchronous = true;
            newEntity.Alias = fromEntity.Alias;
            newEntity.Alt = fromEntity.Alt;
            newEntity.Description = fromEntity.Description;
            newEntity.exName = fromEntity.exName;
            newEntity.FileName = fromEntity.FileName;
            newEntity.FileUrl = fromEntity.FileUrl;
            newEntity.IsActive = fromEntity.IsActive;
            newEntity.Order = fromEntity.Order;
            newEntity.PreViewImage = fromEntity.PreViewImage;
            newEntity.Scoure = fromEntity.Scoure;
            newEntity.Size = fromEntity.Size;
            newEntity.ContentId = input.AssociateId;
            newEntity.Title = fromEntity.Title;
            newEntity.LinkUrl = fromEntity.LinkUrl;
            newEntity.SourceID = fromEntity.Id;
            newEntity.Language = input.TargetLanguage;
            newEntity.TenantId = input.ToTenantId;
            newEntity.Sha1 = fromEntity.Sha1;
            newEntity.IsFromLibrary = fromEntity.IsFromLibrary;
            newEntity.FileLength = fromEntity.FileLength;
            _fileObjectRepository.Insert(newEntity);
            return id;
        }

        public Guid UpdateFileObjectSync(FileObject toEntity, FileObject fromEntity)
        {
            toEntity.Alias = fromEntity.Alias;
            toEntity.Alt = fromEntity.Alt;
            toEntity.Description = fromEntity.Description;
            toEntity.exName = fromEntity.exName;
            toEntity.FileName = fromEntity.FileName;
            toEntity.FileUrl = fromEntity.FileUrl;
            toEntity.IsActive = fromEntity.IsActive;
            toEntity.Order = fromEntity.Order;
            toEntity.PreViewImage = fromEntity.PreViewImage;
            toEntity.Scoure = fromEntity.Scoure;
            toEntity.Size = fromEntity.Size;
            toEntity.Title = fromEntity.Title;
            toEntity.LinkUrl = fromEntity.LinkUrl;
            toEntity.IsDeleted = fromEntity.IsDeleted;
            toEntity.Sha1 = fromEntity.Sha1;
            toEntity.IsFromLibrary = fromEntity.IsFromLibrary;
            toEntity.FileLength = fromEntity.FileLength;

            _fileObjectRepository.Update(toEntity);
            return toEntity.Id;
        }
    }
}
